package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.common.util.CreateQRcode;
import cn.edu.lzu.entity.*;
import cn.edu.lzu.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: UserController
 * @Description: TODO 用户控制器，与用户信息有关的操作
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/2/25 22:11
 * @Version: 1.0
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 提交在“医院挂号”的基本信息
     * @Date: 2020/2/25 23:30
     * @Param: [user, examineDate]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/hospitalSubmitUserMsg")
    public ResponseResult hospitalSubmitUserMsg(User user, Date examineDate, Integer examinePackage) {
        System.out.println("user:" + user + "\t" + "examineDate:" + examineDate + "\t examinePackage:" + examinePackage);
        Session session = SecurityUtils.getSubject().getSession();
        PrincipalCollection principalCollection = (PrincipalCollection) (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
        Examiner examiner = (Examiner) principalCollection.getPrimaryPrincipal();
        ResponseResult responseResult = userService.hospitalSubmitUserMsg(user, examiner, examinePackage, examineDate);
        System.out.println("responseResult:"+responseResult);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 产生用户二维码及完成发送
     * @Date: 2020/2/29 23:20
     * @Param: [userIdentity, response]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/getUserQRcode")
    public ResponseResult getUserQRcode(String userIdentity, int id, Date bookDate, HttpServletResponse response) throws Exception{
        System.out.println("userIdentity = " + userIdentity + ", id = " + id + ", bookDate = " + bookDate);
        ResponseResult responseResult=userService.sendEmail(userIdentity, id, bookDate);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/getUserMsgById")
    public Map<String, Object> getUserMsgById(String userIdentity){
        User user=userService.getUserByUserIdentity(userIdentity);
        Map<String, Object> map=new HashMap<>();
        if(user==null){
            map.put("code", 0);
            map.put("user", null);
        }else{
            map.put("code", 1);
            map.put("user", user);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/submitUserMsg")
    public ResponseResult submitUserMsg(PastDiseaseHistory pastDiseaseHistory, PresentHealthCondition presentHealthCondition, LiveHabit liveHabit, User user, FamilyDiseaseHistory familyDiseaseHistory){
        System.out.println("pastDiseaseHistory = " + pastDiseaseHistory + ",\n presentHealthCondition = " + presentHealthCondition + ", \n liveHabit = " + liveHabit + ",\n user = " + user + ",\n familyDiseaseHistory = " + familyDiseaseHistory);
        ResponseResult responseResult=userService.setUserHealthMsg(user, liveHabit, pastDiseaseHistory, presentHealthCondition, familyDiseaseHistory);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/findUserPwd")
    public ResponseResult findUserPwd(String userName, String userIdentity, String email){
        System.out.println("userName = " + userName + ", userIdentity = " + userIdentity + ", email = " + email);
        ResponseResult responseResult=userService.findPwd(userName, userIdentity, email);
        return responseResult;
    }
}
